<?php

namespace app\xcxapi\service;

use app\api\service\WxpayService;
use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\ContractModel;
use app\models\MemberModel;
use think\Log;

class XcxpayService extends BaseService
{
	/**
	 * 执行支付
	 * User: wufeng
	 * Date: 2020/04/14
	 *
	 * @param $params
	 * @return array
	 */
    public function executionPayment($params)
    {
        try {
            $user = (new MemberModel())->get(['user_id' => xcxapi_get_current_user_id()]);

            $payinfo['bodys'] = '校务帮';
            $payinfo['totalprice'] = $params['amountcollected'] * 100;
            $payinfo['notify'] = 'https://'.$_SERVER['HTTP_HOST'].'/api/notify/wxXcxNotify';
            $payinfo['order_code'] = $params['receipt_number'];
            $payinfo['userid'] = $user['openid'];
			$arr = array(
				'eduadmin_id' => $params['eduadmin_id'],
				'checked_ids' => $params['checked_ids']
			);
            $payinfo['attach'] = implode(',',$arr);
            $res = (new WxpayService())->xcxCheckValid($payinfo);

            if ((isset($res['sj']['result_code']) && 'SUCCESS' !== $res['sj']['result_code']) || (isset($res['sj']['return_code']) && 'SUCCESS' !== $res['sj']['return_code'])) {
                throw new BaseException($res['sj']['err_code_des']);
            }
            $this->data = $res;
        } catch (\Exception $e) {
            $errorMsg = $e->getMessage().' 在 '.$e->getFile().' '.$e->getLine().' 行'.$e->getTraceAsString();
            Log::error($errorMsg);
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }
}
